package com.dennisstepp.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class SuperheroDbAdapter {
	// Database fields
	public static final String KEY_ROWID = " _id";
	public static final String KEY_NAME = "Name";
	public static final String KEY_YEAR = "Year";
	public static final String KEY_VALUE = "Value";
	public static final String KEY_STATUS = "Status";
	private static final String DATABASE_TABLE = "Superhero";

	private Context context;
	private SQLiteDatabase database;
	private DatabaseHelper dbHelper;

	public SuperheroDbAdapter(Context context) {
		this.context = context;
	}

	public SuperheroDbAdapter open() throws SQLException {
		dbHelper = new DatabaseHelper(context);

		database = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	public Cursor fetchAllSuperhero() {
		return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_NAME, KEY_YEAR, KEY_VALUE, KEY_STATUS }, null, null, null,
				null, null);

	}

	public void updateSuperhero(int id, String status) {
		ContentValues values = new ContentValues();
		values.put(KEY_ROWID, status);
		database.update(DATABASE_TABLE, values, KEY_ROWID + "=" + id, null);
	}

}
